package indi.zn.script.groovy;

import java.util.Map;

import groovy.lang.Binding;
import indi.zn.commons.collection.MapType;
import indi.zn.commons.collection.Maps;


public class GroovyContext extends Binding{

	private Map<String,Class<?>> classMap;
	private Map<Object,Object> globalMap; 
	private ThreadLocal<Map<Object, Object>> threadMap=new ThreadLocal<Map<Object,Object>>();
	
	
	public GroovyContext() {
	 
		super(Maps.newMap());
		classMap=Maps.newMap(MapType.ConcurrentMap);
		globalMap=Maps.newMap(MapType.ConcurrentMap);
		// TODO Auto-generated constructor stub
		
		setVariable("global", globalMap);
		setVariable("thread", threadMap);
		
	}

	
	public Class<?> getClassByName(String scriptName){
		
		return classMap.get(scriptName);
	}
	
	public void putClass(String scriptName,Class<?> clazz){
		
		classMap.put(scriptName, clazz);
	}
	
	
	public Object getGlobalVariable(Object key){
		return globalMap.get(key);
	}
	
	public void  putGlobalVariable(Object key,Object value){
		globalMap.put(key, value);
	}
	
	
    public Map<Object, Object>	getThreadVariables(){
		return threadMap.get();
	}
    
    public void setThreadVariables(Map<Object,Object> variableMap){
    	threadMap.set(variableMap);
    }
    
    public void putThreadVariable(Object key,Object value){
    	
    	Map<Object,Object> threadmap=threadMap.get();
    	
    	if(threadmap==null){
    		threadmap=Maps.newMap();
    		threadMap.set(threadmap);
    	}
    	threadmap.put(key, value);
    	}
    
	
}
